package org.springblade.modules.restaurant.job;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.restaurant.service.IExpensesBudgetService;
import org.springframework.stereotype.Component;

/**
 * @author harry
 * @公众号 Harry技术
 */

@Slf4j
@Component
@AllArgsConstructor
@JobExecutor(name = "expensesBudgetJobExecutor")
public class ExpensesBudgetJobExecutor {
	private final IExpensesBudgetService expensesBudgetService;

	public ExecuteResult jobExecute(JobArgs jobArgs) {
		log.info("ExpensesBudgetJobExecutor jobArgs:{}", jobArgs);
		DateTime now = DateUtil.date();
		String nowMonth = DateUtil.format(now, DatePattern.NORM_MONTH_PATTERN);
		// 获取上个月
		String previousMonth = DateUtil.format(DateUtil.offsetMonth(now, -1), DatePattern.NORM_MONTH_PATTERN);
		expensesBudgetService.gen(nowMonth, previousMonth);
		return ExecuteResult.success();
	}


//	@PostConstruct
	public void init() {
		log.info("生成预算支出表任务启动");
		DateTime now = DateUtil.date();
		String nowMonth = DateUtil.format(now, DatePattern.NORM_MONTH_PATTERN);
		// 获取上个月
		String previousMonth = DateUtil.format(DateUtil.offsetMonth(now, -1), DatePattern.NORM_MONTH_PATTERN);
		expensesBudgetService.gen(nowMonth, previousMonth);
	}
}
